<template>
  <el-container class="home-contain">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="primary" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 菜单 -->
        <el-menu
          :router="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          unique-opened
          background-color="#333744"
          text-color="#fff"
          :default-active="active"
          active-text-color="#499CFF"
        >
          <!-- 一级菜单 -->
          <el-submenu
            v-for="item in menulist"
            :key="item.id"
            :index="item.id + ''"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span class="iconfont">{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="subItem in item.children"
              :key="subItem.id"
              :index="'/' + subItem.path"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 被激活的链接地址
      active: "",
      isCollapse: false,
      menulist: [],
      iconsObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
    };
  },
  created() {
    this.getMenuList();
    this.active = window.sessionStorage.getItem("activePath");
  },
  methods: {
    // 保存链接的激活状态
    // 保存当前点击导航栏的index，为了刷新后默认选中
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
    },
    // 折叠侧边导航栏
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    logout() {
      window.sessionStorage.clear("token");
      this.$router.push("/login");
    },
    // 得到侧边导航的数据
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
      console.log(res);
    },
  },
};
</script>
<style lang="less" scoped>
.home-contain {
  height: 100%;
}
.el-header {
  display: flex;
  padding-left: 0;
  align-items: center;
  justify-content: space-between;
  background-color: #373c41;
  font-size: 20px;
  color: #fff;
  > div {
    display: flex;
    justify-content: center;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf2;
}
.iconfont {
  margin-left: 10px;
}
.toggle-button {
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  background-color: #485166;
  cursor: pointer;
}
</style>>